/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "SqlUpdateBuilder.h"

#include "SqlServer.h"

using namespace Bio;

/**
**/
SqlUpdateBuilder::SqlUpdateBuilder( const QString &table, const QString &condition, SqlServer *server ) : m_server( server ),
	m_firstField( false ), m_condition( condition )
{
	m_sql = QString( "UPDATE %1 SET" ).arg( table );
}

/**
**/
SqlUpdateBuilder::~SqlUpdateBuilder()
{
}

/**
**/
bool SqlUpdateBuilder::addValue( const QString &field, int val )
{
	return add( field, QString::number( val ) );
}

/**
**/
bool SqlUpdateBuilder::addValue( const QString &field, const QString &val )
{
	return add( field, m_server->esc( val ) );
}

/**
**/
bool SqlUpdateBuilder::addNullValue( const QString &field )
{
	return add( field, "NULL" );
}

/**
**/
bool SqlUpdateBuilder::exec()
{
	if( !m_condition.isEmpty() )
		m_sql += " " + m_condition;

	return m_server->exec( m_sql );
}

/**
**/
bool SqlUpdateBuilder::add( const QString &field, const QString &val )
{
	if( m_firstField )
		m_sql += ",";
	else
		m_firstField = true;

	m_sql += " " + field + " = " + val;

	return true;
}
